<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Kat_Produk extends CI_Controller {

	function __construct()
	{
		parent::__construct();
		$this->load->model('Produk_model');
		$this->load->helper('bantuan_helper');
		if(!$this->session->userdata('user_id') && !$this->session->userdata('user_display_name') ):
		redirect('backend');
		endif;
	}

	function index()
	{
		if(!$this->session->userdata('filter_brand')):
			$this->session->set_userdata('filter_brand','all');
		endif;
		$this->load->library('pagination');
		$offset = $this->uri->segment(4);
		$config['base_url'] = site_url().'/backend/kat_produk/index';
		$config['per_page'] = $this->config->item('per_page');
		$config['uri_segment'] = '4';
		if($this->session->userdata('filter_brand')=='all'):
			$config['total_rows'] = count($this->Produk_model->getKatProduk('all',FALSE,FALSE,FALSE));
			$data['res'] = $this->Produk_model->getKatProduk('all',FALSE,$config['per_page'],$offset);
		else:
			$config['total_rows'] = count($this->Produk_model->getKatProduk('brand',$this->session->userdata('filter_brand'),FALSE,FALSE));
			$data['res'] = $this->Produk_model->getKatProduk('brand',$this->session->userdata('filter_brand'),$config['per_page'],$offset);
		endif;
		$data['urut'] = $this->uri->segment(4);
		$data['title'] = 'Kategori Produk';
		$data['template'] = 'kat_produk/index';
		
		
		$this->pagination->initialize($config);
		$this->load->view('backend/index',$data);
	}

	function add()
	{
		$this->form_validation->set_rules('nama_kategori', 'Nama','required');
		$this->form_validation->set_rules('deskripsi', 'Deskripsi','required');
		
		if ($this->form_validation->run() == FALSE)
		{
			$data['title'] = 'Kategori Produk';
			$data['template'] = 'kat_produk/add';
			$this->load->view('backend/index',$data);
		}
		else
		{
			$data = array(
						'nama_kategori'=>$this->input->post('nama_kategori'),
						'deskripsi'=>$this->input->post('deskripsi'),
						'brand_id'=>$this->input->post('prod_brand_id'),
						'kat_level'=>$this->input->post('kat_level'),
						'parent_kat_id'=>$this->input->post('parent_kat_id'),
						'urut'=>$this->input->post('urut'),
						'caps_posisi'=>$this->input->post('caps_posisi')
						);
			$this->Produk_model->addKatProduk($data);
			$this->session->set_flashdata('message_type','<div id="kotak" class="success">Satu data berhasil ditambahkan</div>');
			redirect('backend/kat_produk');
		}
	}
	
	function edit()
	{
		$this->form_validation->set_rules('nama_kategori', 'Nama','required');
		$this->form_validation->set_rules('deskripsi', 'Deskripsi','required');
		
		if ($this->form_validation->run() == FALSE)
		{
			$id = $this->uri->segment(4);
			$data['row'] = $this->Produk_model->getKatProduk(FALSE,$id,FALSE,FALSE);
			$data['title'] = 'Kategori Produk';
			$data['template'] = 'kat_produk/edit';
			$this->load->view('backend/index',$data);
		}
		else
		{
			$id = $this->uri->segment(4);
			$data = array(
						'nama_kategori'=>$this->input->post('nama_kategori'),
						'deskripsi'=>$this->input->post('deskripsi'),
						'brand_id'=>$this->input->post('prod_brand_id'),
						'kat_level'=>$this->input->post('kat_level'),
						'parent_kat_id'=>$this->input->post('parent_kat_id'),
						'urut'=>$this->input->post('urut'),
						'caps_posisi'=>$this->input->post('caps_posisi')
						);
			$this->Produk_model->editKatProduk($id,$data);
			$this->session->set_flashdata('message_type','<div id="kotak" class="success">Data berhasil diupdate</div>');
			redirect('backend/kat_produk/edit/'.$id);
		}
	}

	function delete()
	{
		$id = $this->uri->segment(4);
		if(!empty($id)):
			$num = 0;
			$cekKategori = $this->db->where('kategori_id',$id)->get('tb_kat_produk')->row();
			if($cekKategori->kat_level=='1'):
				$kategories = $this->db->where('parent_kat_id',$cekKategori->kategori_id)->get('tb_kat_produk')->result();
				foreach($kategories as $kategori):
					$kategories2 = $this->db->where('parent_kat_id',$kategori->kategori_id)->get('tb_kat_produk')->result();
					foreach($kategories2 as $kategori2):
						$numproduct = $this->db->where('prod_kat_id',$kategori2->kategori_id)->count_all_results('tb_produk');
						$num = $num+$numproduct;
					endforeach;
				endforeach;
			elseif($cekKategori->kat_level=='2'):
				$kategories = $this->db->where('parent_kat_id',$cekKategori->kategori_id)->get('tb_kat_produk')->result();
				foreach($kategories as $kategori):
					$numproduct = $this->db->where('prod_kat_id',$kategori2->kategori_id)->count_all_results('tb_produk');
					$num = $num+$numproduct;
				endforeach;
			elseif($cekKategori->kat_level=='3'):
				$num = $this->db->where('prod_kat_id',$kategori2->kategori_id)->count_all_results('tb_produk');
			endif;
			if($num>0):
				$this->session->set_flashdata('message_type','<div id="kotak" class="error">Masih ada produk dalam kategori ini</div>');
				redirect('backend/kat_produk','refresh');
			else:
				$this->db->where('kategori_id',$id);
				$this->db->delete('tb_kat_produk');
				$this->session->set_flashdata('message_type','<div id="kotak" class="success">Satu data berhasil dihapus</div>');
				redirect('backend/kat_produk','refresh');
			endif;
		else:
			redirect('backend/kat_produk','refresh');
		endif;
	}
	
	function ajax_bulk_action()
	{
		foreach($_POST["kategori_id"] as $id):
			$num = 0;
			$cekKategori = $this->db->where('kategori_id',$id)->get('tb_kat_produk')->row();
			if($cekKategori->kat_level=='1'):
				$kategories = $this->db->where('parent_kat_id',$cekKategori->kategori_id)->get('tb_kat_produk')->result();
				foreach($kategories as $kategori):
					$kategories2 = $this->db->where('parent_kat_id',$kategori->kategori_id)->get('tb_kat_produk')->result();
					foreach($kategories2 as $kategori2):
						$numproduct = $this->db->where('prod_kat_id',$kategori2->kategori_id)->count_all_results('tb_produk');
						$num = $num+$numproduct;
					endforeach;
				endforeach;
			elseif($cekKategori->kat_level=='2'):
				$kategories = $this->db->where('parent_kat_id',$cekKategori->kategori_id)->get('tb_kat_produk')->result();
				foreach($kategories as $kategori):
					$numproduct = $this->db->where('prod_kat_id',$kategori2->kategori_id)->count_all_results('tb_produk');
					$num = $num+$numproduct;
				endforeach;
			elseif($cekKategori->kat_level=='3'):
				$num = $this->db->where('prod_kat_id',$kategori2->kategori_id)->count_all_results('tb_produk');
			endif;
			if($num>0):
				$this->session->set_flashdata('message_type','<div id="kotak" class="error">Masih ada produk dalam kategori ini</div>');
			else:
				$this->db->where('kategori_id',$id);
				$this->db->delete('tb_kat_produk');
			endif;
		endforeach;
	}
	
	function filter_brand($brand_id)
	{
		if($brand_id):
			$this->session->set_userdata('filter_brand',$brand_id);
			redirect('backend/kat_produk');
		else:
			redirect('backend/kat_produk','refresh');
		endif;
	}
	
	function select_category()
	{
		if($_POST):
			$brand_id = $_POST['brand_id'];
			$kat_level = $_POST['kat_level'];
			echo select_parent_category($kat_level, $brand_id);
		endif;
	}
}

/* End of file product_cat.php */
/* Location: ./system/application/controllers/backend/product_cat.php */
